﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System.Collections.Generic;
using NetBay.Core.BusinessEntities;
using System.Data.Common;
using System;
using NetBay.Core.Log;
using System.Data;
#endregion


namespace Rio.Doc.Business.Repository.Translator
{
    /// <summary>
    /// Class used to convert Alertes database row to alert object
    /// </summary>
    public class AlertTranslator
    {
        /// <summary>
        /// Translate a bunch of Group DataReader to Group object
        /// </summary>
        /// <param name="P_oDbDataReader">The dataReader that contains the rows</param>
        /// <returns>A list of Group Object</returns>
        public static List<Alert> TranslateAlert(IDataReader P_oDbDataReader)
        {
            var l_lstAlerts = new List<Alert>();
            try
            {
                using (P_oDbDataReader)
                {
                    while (P_oDbDataReader.Read())
                    {
                        TranslateOneAlert(P_oDbDataReader, l_lstAlerts);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_lstAlerts;
        }


        private static void TranslateOneAlert(IDataReader p_oIDataReader, List<Alert> l_lstAlerts)
        {
            var l_oAlert = new Alert();

            try
            {
                l_oAlert.Identifier = Convert.ToInt32(p_oIDataReader["ALERT_ID"]);
                l_oAlert.Name = p_oIDataReader["ALERT_NAME"].ToString();
                l_oAlert.Object = p_oIDataReader["ALERT_OBJET"].ToString();
                l_oAlert.Body = p_oIDataReader["ALERT_BODY"].ToString();
                l_oAlert.Date = ConvertField.ToDateTime(p_oIDataReader["ALERT_DATE"].ToString());
                l_oAlert.UserCreationIdentifier = Convert.ToInt32(p_oIDataReader["ALERT_USERCRE"]);
                l_oAlert.CreationDate = ConvertField.ToDateTime(p_oIDataReader["ALERT_DATECRE"].ToString());
                if (p_oIDataReader["ALERT_SEND_DATE"] != null &&
                    !string.Empty.Equals(p_oIDataReader["ALERT_SEND_DATE"].ToString()))
                    l_oAlert.SendDate = ConvertField.ToDateTime(p_oIDataReader["ALERT_SEND_DATE"].ToString());
                l_oAlert.StatusIdentifier = Convert.ToInt32(p_oIDataReader["ALERT_STATUT_ID"]);
                l_oAlert.StatusName = p_oIDataReader["ALERT_STATUT_NAME"].ToString();
                l_oAlert.UserEmail = p_oIDataReader["ruser_email"].ToString();

                if(p_oIDataReader["ALERT_ACTION_ID"] != DBNull.Value)
                    l_oAlert.ActionId = Convert.ToInt32(p_oIDataReader["ALERT_ACTION_ID"].ToString());

                l_oAlert.ActionLabel = p_oIDataReader["ALERT_ACTION_NAME"].ToString();

                l_oAlert.IsValid = true;
                // All members are readed, so save this document to collection
                l_lstAlerts.Add(l_oAlert);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }
        }


    }
}
